package com.basic.java.threads.yield;

/**
 * 类
 * 创建时间：2022/4/18 22:59
 *
 * @author gejin
 */
public class TestYield {
    public static void main(String[] args) {
        Yeild yeild1 = new Yeild("a");
        Yeild yeild2 = new Yeild("b");

        new Thread(yeild1).start();
        new Thread(yeild2).start();
    }
}

// 礼让不一定成功， 主要看CPU的调度情况
class Yeild implements Runnable {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Yeild(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(getName() + "开始执行");
        Thread.yield(); // 线程礼让
        System.out.println(getName() + "停止");
    }
}
